package com.homestead.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author HanBin_Yang
 * @since 2021/5/29 23:13
 */
public class RedisSEQTimer {
    private static final Logger logger = LoggerFactory.getLogger(RedisSEQTimer.class);
    public static final String DAY_FORMAT_PATTERN = "yyMMdd";
    public static volatile RedisSEQTimer redisSEQTimer;
    private final ConcurrentHashMap<String, Long> REDIS_INCR_MAP = new ConcurrentHashMap();
    private final String _DEFAULT_ZK_NAMESPACE = "/OSEQ_REDIS";
    private long _REDIS_MAXVALUE_INIT = 10000L;
    private Timer _TIMER = new Timer(true);
    private volatile boolean _CLEAN_STATUS;
    private static final String _REMOVE_KEY = "OSEQ_REMOVE_KEY";

    public static void close(Closeable... closeAbles) {
        if (closeAbles != null && closeAbles.length > 0) {
            Closeable[] var1 = closeAbles;
            int var2 = closeAbles.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                Closeable closeAble = var1[var3];
                if (closeAble != null) {
                    try {
                        closeAble.close();
                    } catch (IOException var6) {
                        logger.error(var6.getMessage(), var6);
                    }
                }
            }

        }
    }

    public static RedisSEQTimer getInstance() {
        if (redisSEQTimer == null) {
            Class var0 = RedisSEQTimer.class;
            synchronized(RedisSEQTimer.class) {
                if (redisSEQTimer == null) {
                    redisSEQTimer = new RedisSEQTimer();
                }
            }
        }

        return redisSEQTimer;
    }

    private Date getFirstTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(11, 24);
        calendar.set(12, this.getRandNum(6, 0));
        calendar.set(13, this.getRandNum(60, 0));
        return calendar.getTime();
    }

    private int getRandNum(int exclude, int from) {
        return (new Random()).nextInt(exclude) + from;
    }

    public synchronized void push(String key, Long val) {
        this.REDIS_INCR_MAP.put(key, val);
    }

    public String getDayFormat() {
        return this.getDayFormat(new Date());
    }

    public String getDayFormat(Date date) {
        return (new SimpleDateFormat("yyMMdd")).format(date);
    }
}

